package com.tongzhan.service;

import com.tongzhan.base.BaseCriteria;
import com.tongzhan.consts.SysDictTypeKeyConst;
import com.tongzhan.entity.SysDict;
import com.tongzhan.entity.User;
import com.tongzhan.entity.party.ActionCenter;
import com.tongzhan.entity.party.NewPerson;
import com.tongzhan.entity.party.Partyinfo;
import com.tongzhan.entity.party.PeopleOpinion;
import com.tongzhan.entity.religion.Street;
import com.tongzhan.entity.xiangxian.ExamplePoint;
import com.tongzhan.repository.*;
import com.tongzhan.repository.party.PeopleOpinionRepository;
import com.tongzhan.repository.religion.StreetRepository;
import com.tongzhan.util.DateUtil;
import com.tongzhan.utils.DateSwitcher;
import com.tongzhan.utils.SnowFlakeUtils;
import com.tongzhan.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

@Service
public class DangpaiService {

    @Autowired
    public ExamplePointRepository examplePointRepository;
    @Autowired
    private PeopleOpinionRepository peopleOpinionRepository;
    @Autowired
    private NewPersonRepository newPersonRepository;
    @Autowired
    private ActionCenterRepository actionCenterRepository;
    @Autowired
    private SysDictRepository sysDictRepository;
    @Autowired
    private ParticipationRepository participationRepository;
    @Autowired
    private DiscussRepository discussRepository;
    @Autowired
    private SysDictService sysDictService;
    @Autowired
    private PartyinfoRepository partyinfoRepository;

    public List<ExamplePoint> getMapList(Integer type, String date) {
        if (type == null) {
            type = 1;
        }
        if (date == null || "".equals(date)) {
            date = DateUtil.getSysYear();
        }
        String startDate = DateUtil.getStartDate(type, date);
        String endDate = DateUtil.getEndDate(type, date);
        BaseCriteria criteria = DateUtil.getDateSearchCriteria(startDate, endDate, "time");

        List<ExamplePoint> list = examplePointRepository.find(new Query().addCriteria(BaseCriteria.where("type").is(1).and("is_del").is(0).andOperator(criteria)), null);
        return list;
    }

    public List<PeopleOpinion> getPeopleOpinionList(Integer type, String date) {
        if (type == null) {
            type = 1;
        }
        if (date == null || "".equals(date)) {
            date = DateUtil.getSysYear();
        }
        String startDate = DateUtil.getStartDate(type, date);
        String endDate = DateUtil.getEndDate(type, date);
        BaseCriteria criteria = DateUtil.getDateSearchCriteria(startDate, endDate, "time");
        Query query = new Query();
        List<PeopleOpinion> list = peopleOpinionRepository.find(query.addCriteria(Criteria.where("is_del").is(0).andOperator(criteria)).with(Sort.by(Sort.Direction.DESC, "time")), null);

        return list;
    }

    public Map<String, Object> getNewPersonList(Integer type, String date) {
        Map<String, Object> map = new HashMap<>();
        List<UserInfoVO> list = new ArrayList<>();
        List<Long> countList = new ArrayList<>();
        if (type == null) {
            type = 1;
        }
        if (date == null || "".equals(date)) {
            date = DateUtil.getSysYear();
        }
        String startDate = DateUtil.getStartDate(type, date);
        String endDate = DateUtil.getEndDate(type, date);
        BaseCriteria criteria = DateUtil.getDateSearchCriteria(startDate, endDate, "addtime");
        // type 党派类型(1:民革德清基层委、2:民盟德清支部、3:民建德清支部、4:民进德清基层委、5:农工党德清基层委、6:九三学社德清基层委)
        List<SysDict> all = sysDictRepository.findAll(new Query().addCriteria(Criteria.where("isDelete").is(0).and("pid").is("486532304131731456")));
        all.forEach(c -> {
            Criteria baseCriteria = BaseCriteria.where("partyTypeId").is(c.getType()).and("is_del").is(0).and("type").is(3);
//            baseCriteria.and("type").is(Integer.valueOf(c.getType()));

            long count = newPersonRepository.count(new Query(baseCriteria.andOperator(criteria)));
            countList.add(count);
            UserInfoVO userInfoVO = new UserInfoVO();
            userInfoVO.setName(c.getValue());
            userInfoVO.setCount(count);
            list.add(userInfoVO);
        });
        long totalCount = 0;
        for (Long l : countList) {
            totalCount = totalCount + l;
        }
        map.put("totalCount", totalCount);
        map.put("dataList", list);
        return map;
    }

    public Map<String, Object> getActionCenterList(Integer type, String date) {
        if (type == null) {
            type = 1;
        }
        if (date == null || "".equals(date)) {
            date = DateUtil.getSysYear();
        }
        String year = date.substring(0, 4);
        String month1Start = year + "-01-01";
        String month2Start = year + "-02-01";
        String month3Start = year + "-03-01";
        String month4Start = year + "-04-01";
        String month5Start = year + "-05-01";
        String month6Start = year + "-06-01";
        String month7Start = year + "-07-01";
        String month8Start = year + "-08-01";
        String month9Start = year + "-09-01";
        String month10Start = year + "-10-01";
        String month11Start = year + "-11-01";
        String month12Start = year + "-12-01";

        String month1End = year + "-01-31";
        String month2End = year + "-02-29";
        String month3End = year + "-03-31";
        String month4End = year + "-04-30";
        String month5End = year + "-05-31";
        String month6End = year + "-06-30";
        String month7End = year + "-07-31";
        String month8End = year + "-08-31";
        String month9End = year + "-09-30";
        String month10End = year + "-10-31";
        String month11End = year + "-11-30";
        String month12End = year + "-12-31";
        Map<String, Object> map = new HashMap<>();
        List<String> dateList = new ArrayList<>();
        List<Long> countList = new ArrayList<>();
        dateList.add("1月");
        dateList.add("2月");
        dateList.add("3月");
        dateList.add("4月");
        dateList.add("5月");
        dateList.add("6月");
        dateList.add("7月");
        dateList.add("8月");
        dateList.add("9月");
        dateList.add("10月");
        dateList.add("11月");
        dateList.add("12月");

        BaseCriteria criteria1 = DateUtil.getDateSearchCriteria(month1Start, month1End, "time");
        BaseCriteria criteria2 = DateUtil.getDateSearchCriteria(month2Start, month2End, "time");
        BaseCriteria criteria3 = DateUtil.getDateSearchCriteria(month3Start, month3End, "time");
        BaseCriteria criteria4 = DateUtil.getDateSearchCriteria(month4Start, month4End, "time");
        BaseCriteria criteria5 = DateUtil.getDateSearchCriteria(month5Start, month5End, "time");
        BaseCriteria criteria6 = DateUtil.getDateSearchCriteria(month6Start, month6End, "time");
        BaseCriteria criteria7 = DateUtil.getDateSearchCriteria(month7Start, month7End, "time");
        BaseCriteria criteria8 = DateUtil.getDateSearchCriteria(month8Start, month8End, "time");
        BaseCriteria criteria9 = DateUtil.getDateSearchCriteria(month9Start, month9End, "time");
        BaseCriteria criteria10 = DateUtil.getDateSearchCriteria(month10Start, month10End, "time");
        BaseCriteria criteria11 = DateUtil.getDateSearchCriteria(month11Start, month11End, "time");
        BaseCriteria criteria12 = DateUtil.getDateSearchCriteria(month12Start, month12End, "time");
        // type 类型 1是党派 2是侨台 3是乡贤
        long count1 = actionCenterRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria1)));
        long count2 = actionCenterRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria2)));
        long count3 = actionCenterRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria3)));
        long count4 = actionCenterRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria4)));
        long count5 = actionCenterRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria5)));
        long count6 = actionCenterRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria6)));
        long count7 = actionCenterRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria7)));
        long count8 = actionCenterRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria8)));
        long count9 = actionCenterRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria9)));
        long count10 = actionCenterRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria10)));
        long count11 = actionCenterRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria11)));
        long count12 = actionCenterRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria12)));
        countList.add(count1);
        countList.add(count2);
        countList.add(count3);
        countList.add(count4);
        countList.add(count5);
        countList.add(count6);
        countList.add(count7);
        countList.add(count8);
        countList.add(count9);
        countList.add(count10);
        countList.add(count11);
        countList.add(count12);
        long totalCount = count1 + count2 + count3 + count4 + count5 + count6 + count7 + count8 + count9 + count10 + count11 + count12;
        map.put("totalCount", totalCount);
        map.put("countList", countList);
        map.put("dateList", dateList);
        return map;
    }


    public Map<String, Object> getParticipationList(Integer type, String date) {
        List<DisCussVO> disCussVOList = new ArrayList<>();
        if (type == null) {
            type = 1;
        }
        if (date == null || "".equals(date)) {
            date = DateUtil.getSysYear();
        }
        String year = date.substring(0, 4);
        String month1Start = year + "-01-01";
        String month2Start = year + "-02-01";
        String month3Start = year + "-03-01";
        String month4Start = year + "-04-01";
        String month5Start = year + "-05-01";
        String month6Start = year + "-06-01";
        String month7Start = year + "-07-01";
        String month8Start = year + "-08-01";
        String month9Start = year + "-09-01";
        String month10Start = year + "-10-01";
        String month11Start = year + "-11-01";
        String month12Start = year + "-12-01";

        String month1End = year + "-01-31";
        String month2End = year + "-02-29";
        String month3End = year + "-03-31";
        String month4End = year + "-04-30";
        String month5End = year + "-05-31";
        String month6End = year + "-06-30";
        String month7End = year + "-07-31";
        String month8End = year + "-08-31";
        String month9End = year + "-09-30";
        String month10End = year + "-10-31";
        String month11End = year + "-11-30";
        String month12End = year + "-12-31";
        Map<String, Object> map = new HashMap<>();
        List<String> dateList = new ArrayList<>();
        List<Long> countList = new ArrayList<>();
        List<Long> countList2 = new ArrayList<>();
        dateList.add("1月");
        dateList.add("2月");
        dateList.add("3月");
        dateList.add("4月");
        dateList.add("5月");
        dateList.add("6月");
        dateList.add("7月");
        dateList.add("8月");
        dateList.add("9月");
        dateList.add("10月");
        dateList.add("11月");
        dateList.add("12月");

        BaseCriteria criteria1 = DateUtil.getDateSearchCriteria(month1Start, month1End, "time");
        BaseCriteria criteria2 = DateUtil.getDateSearchCriteria(month2Start, month2End, "time");
        BaseCriteria criteria3 = DateUtil.getDateSearchCriteria(month3Start, month3End, "time");
        BaseCriteria criteria4 = DateUtil.getDateSearchCriteria(month4Start, month4End, "time");
        BaseCriteria criteria5 = DateUtil.getDateSearchCriteria(month5Start, month5End, "time");
        BaseCriteria criteria6 = DateUtil.getDateSearchCriteria(month6Start, month6End, "time");
        BaseCriteria criteria7 = DateUtil.getDateSearchCriteria(month7Start, month7End, "time");
        BaseCriteria criteria8 = DateUtil.getDateSearchCriteria(month8Start, month8End, "time");
        BaseCriteria criteria9 = DateUtil.getDateSearchCriteria(month9Start, month9End, "time");
        BaseCriteria criteria10 = DateUtil.getDateSearchCriteria(month10Start, month10End, "time");
        BaseCriteria criteria11 = DateUtil.getDateSearchCriteria(month11Start, month11End, "time");
        BaseCriteria criteria12 = DateUtil.getDateSearchCriteria(month12Start, month12End, "time");
        long count1 = participationRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria1)));
        long count2 = participationRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria2)));
        long count3 = participationRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria3)));
        long count4 = participationRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria4)));
        long count5 = participationRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria5)));
        long count6 = participationRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria6)));
        long count7 = participationRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria7)));
        long count8 = participationRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria8)));
        long count9 = participationRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria9)));
        long count10 = participationRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria10)));
        long count11 = participationRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria11)));
        long count12 = participationRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria12)));
        countList.add(count1);
        countList.add(count2);
        countList.add(count3);
        countList.add(count4);
        countList.add(count5);
        countList.add(count6);
        countList.add(count7);
        countList.add(count8);
        countList.add(count9);
        countList.add(count10);
        countList.add(count11);
        countList.add(count12);

        long discusscount1 = discussRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria1)));
        long discusscount2 = discussRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria2)));
        long discusscount3 = discussRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria3)));
        long discusscount4 = discussRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria4)));
        long discusscount5 = discussRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria5)));
        long discusscount6 = discussRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria6)));
        long discusscount7 = discussRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria7)));
        long discusscount8 = discussRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria8)));
        long discusscount9 = discussRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria9)));
        long discusscount10 = discussRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria10)));
        long discusscount11 = discussRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria11)));
        long discusscount12 = discussRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteria12)));
        countList2.add(discusscount1);
        countList2.add(discusscount2);
        countList2.add(discusscount3);
        countList2.add(discusscount4);
        countList2.add(discusscount5);
        countList2.add(discusscount6);
        countList2.add(discusscount7);
        countList2.add(discusscount8);
        countList2.add(discusscount9);
        countList2.add(discusscount10);
        countList2.add(discusscount11);
        countList2.add(discusscount12);
//        List<String> dataList = new ArrayList<>();
//        dataList.add("发布");
//        dataList.add("评论");
        DisCussVO disCussVO = new DisCussVO();
        disCussVO.setName("发布");
        disCussVO.setData(countList);
        disCussVOList.add(disCussVO);
        DisCussVO disCussVO2 = new DisCussVO();
        disCussVO2.setName("评论");
        disCussVO2.setData(countList2);
        disCussVOList.add(disCussVO2);
//        map.put("dataList",dataList);
//        map.put("countList1",countList);
//        map.put("countList2",countList2);
        map.put("countList", disCussVOList);
        map.put("dateList", dateList);


        //参政
        BaseCriteria criteriaCzYear = DateUtil.getDateSearchCriteria(month1Start, month12End, "time");
        long yearCzcount = participationRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteriaCzYear)));
        map.put("canzheng", yearCzcount);

        //回复
        long yearHfcount = discussRepository.count(new Query(Criteria.where("is_del").is(0)).addCriteria(BaseCriteria.where("type").is(1).andOperator(criteriaCzYear)));
        map.put("huifu", yearHfcount);


        return map;
    }

    public List<UserInfoVO> personCount(String streetCode, String villageCode) {
        List<UserInfoVO> list = new ArrayList<>();
        //查询所有党派 type 党派类型(1:民革德清基层委、2:民盟德清支部、3:民建德清支部、4:民进德清基层委、5:农工党德清基层委、6:九三学社德清基层委)
        List<SysDict> all = sysDictRepository.findAll(new Query().addCriteria(Criteria.where("isDelete").is(0).and("pid").is("486532304131731456")));
        all.forEach(sysDict -> {
            String type = sysDict.getType();
            String name = sysDict.getValue();
            //查询条件
            Query query = new Query().addCriteria(Criteria.where("is_del").is(0).and("partyTypeId").is(Integer.valueOf(type)).and("indicate_result").is(1));
            if (streetCode != null && streetCode.length() > 0 && !streetCode.equals("")) {
                query.addCriteria(Criteria.where("streetCode").is(streetCode));
            }
            if (villageCode != null && villageCode.length() > 0 && !villageCode.equals("")) {
                query.addCriteria(Criteria.where("villageCode").is(villageCode));
            }
            long count = newPersonRepository.count(query);
            UserInfoVO userInfoVO = new UserInfoVO();
            userInfoVO.setName(name);
            userInfoVO.setCount(count);
            list.add(userInfoVO);
        });
        return list;
    }

    public List<PersonDetailVO> personDetail(String streetCode,String villageCode) {
        List<PersonDetailVO> list = new ArrayList<>();
        //查询条件
        Query query = new Query(Criteria.where("is_del").is(0)).addCriteria(Criteria.where("indicate_result").is(1));
        if (streetCode != null && streetCode.length() > 0 && !streetCode.equals("")) {
            query.addCriteria(Criteria.where("streetCode").is(streetCode));
        }
        if (villageCode != null && villageCode.length() > 0 && !villageCode.equals("")) {
            query.addCriteria(Criteria.where("villageCode").is(villageCode));
        }
        //查询党派中所有人员
        List<NewPerson> personList = newPersonRepository.findAll(query);
        //遍历每个人的信息返回
        personList.forEach(newPerson -> {
            PersonDetailVO personDetailVO = new PersonDetailVO();
            personDetailVO.setName(newPerson.getName());
            personDetailVO.setPartyTypeName(sysDictService.getValueByType(SysDictTypeKeyConst.PUBLIC_PARTY_TYPE, newPerson.getPartyTypeId()));
            personDetailVO.setGender(newPerson.getGender());
            personDetailVO.setTelephone(newPerson.getTelephone());
            list.add(personDetailVO);
        });
        return list;
    }

    public List<MonthCountVO> actionCenterCount(String streetCode,String villageCode) {
        ArrayList<MonthCountVO> resultList = new ArrayList<>();
        //当前时间
        LocalDateTime now = LocalDateTime.now();
        //当年的第一天
        LocalDateTime januaryTime = now.with(TemporalAdjusters.firstDayOfYear());
        for (int i = 0; i < 12; i++) {
            LocalDateTime time = januaryTime.plusMonths(i);
            //查询条件
            Query query = new Query().
                    addCriteria(
                            Criteria.where("time").gte(DateSwitcher.getMinTime(time)).
                                    andOperator(Criteria.where("time").lte(DateSwitcher.getMaxTime(time.with(TemporalAdjusters.lastDayOfMonth())))).
                                    and("type").is(1).and("is_del").is(0)
                    );
            if (streetCode != null && streetCode.length() > 0 && !streetCode.equals("")) {
                query.addCriteria(Criteria.where("streetCode").is(streetCode));
            }
            if (villageCode != null && villageCode.length() > 0 && !villageCode.equals("")) {
                query.addCriteria(Criteria.where("villageCode").is(villageCode));
            }
            //查询每月活动数量
            long count = actionCenterRepository.count(query);
            MonthCountVO monthCountVO = new MonthCountVO();
            monthCountVO.setMonth(time.getMonthValue());
            monthCountVO.setCount(count);
            resultList.add(monthCountVO);
        }
        return resultList;
    }


    public List<ActionCenterDetailVO> actionCenterDetail(String streetCode,String villageCode) {
        ArrayList<ActionCenterDetailVO> resultList = new ArrayList<>();
        //获取今年的开始时间和结束时间
        LocalDateTime now = LocalDateTime.now();
        int year = now.getYear();
        LocalDateTime startTime = LocalDateTime.of(year, 1, 1, 00, 00, 00);
        LocalDateTime endTime = LocalDateTime.of(year, 12, 31, 23, 59, 59);
        //查询条件
        Query query = new Query().addCriteria(
                Criteria.where("time").gte(startTime).
                        andOperator(Criteria.where("time").lte(endTime)).
                        and("type").is(1).and("is_del").is(0)).
                with(Sort.by(Sort.Direction.ASC, "time"));
        if (streetCode != null && streetCode.length() > 0 && !streetCode.equals("")) {
            query.addCriteria(Criteria.where("streetCode").is(streetCode));
        }
        if (villageCode != null && villageCode.length() > 0 && !villageCode.equals("")) {
            query.addCriteria(Criteria.where("villageCode").is(villageCode));
        }
        //查询今年所有的活动
        List<ActionCenter> actionCenterList = actionCenterRepository.findAll(query);
        actionCenterList.forEach(actionCenter -> {
            ActionCenterDetailVO actionCenterDetailVO = new ActionCenterDetailVO();
            actionCenterDetailVO.setName(actionCenter.getName());
            actionCenterDetailVO.setAddress(actionCenter.getAction_address());
            Date time = actionCenter.getTime();
            actionCenterDetailVO.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time));
            actionCenterDetailVO.setActionTypeName(sysDictService.getValueByType(SysDictTypeKeyConst.PUBLIC_ACTION_TYPE, actionCenter.getActionTypeId()));
            resultList.add(actionCenterDetailVO);
        });
        return resultList;
    }


    public List<MonthCountVO> opinionCount(String streetCode,String villageCode) {
        ArrayList<MonthCountVO> resultList = new ArrayList<>();
        //当前时间
        LocalDateTime now = LocalDateTime.now();
        //当年的第一天
        LocalDateTime januaryTime = now.with(TemporalAdjusters.firstDayOfYear());
        for (int i = 0; i < 12; i++) {
            LocalDateTime time = januaryTime.plusMonths(i);
            //查询条件
            Query query = new Query().
                    addCriteria(
                            Criteria.where("time").gte(DateSwitcher.getMinTime(time)).
                                    andOperator(Criteria.where("time").lte(DateSwitcher.getMaxTime(time.with(TemporalAdjusters.lastDayOfMonth())))).
                                    and("type").is(1).and("is_del").is(0)
                    );
            if (streetCode != null && streetCode.length() > 0 && !streetCode.equals("")) {
                query.addCriteria(Criteria.where("streetCode").is(streetCode));
            }
            if (villageCode != null && villageCode.length() > 0 && !villageCode.equals("")) {
                query.addCriteria(Criteria.where("villageCode").is(villageCode));
            }
            //查询每月活动数量
            long count = peopleOpinionRepository.count(query);
            MonthCountVO monthCountVO = new MonthCountVO();
            monthCountVO.setMonth(time.getMonthValue());
            monthCountVO.setCount(count);
            resultList.add(monthCountVO);
        }
        return resultList;
    }

    public List<OpinionDetailVO> opinionDetail(String streetCode,String villageCode) {
        ArrayList<OpinionDetailVO> resultList = new ArrayList<>();
        //获取今年的开始时间和结束时间
        LocalDateTime now = LocalDateTime.now();
        int year = now.getYear();
        LocalDateTime startTime = LocalDateTime.of(year, 1, 1, 00, 00, 00);
        LocalDateTime endTime = LocalDateTime.of(year, 12, 31, 23, 59, 59);
        //查询条件
        Query query = new Query().addCriteria(
                Criteria.where("time").gte(startTime).
                        andOperator(Criteria.where("time").lte(endTime)).
                        and("type").is(1).and("is_del").is(0)).
                with(Sort.by(Sort.Direction.ASC, "time"));
        if (streetCode != null && streetCode.length() > 0 && !streetCode.equals("")) {
            query.addCriteria(Criteria.where("streetCode").is(streetCode));
        }
        if (villageCode != null && villageCode.length() > 0 && !villageCode.equals("")) {
            query.addCriteria(Criteria.where("villageCode").is(villageCode));
        }
        //查询今年所有的社情名义
        List<PeopleOpinion> peopleOpinionList = peopleOpinionRepository.findAll(query);
        peopleOpinionList.forEach(peopleOpinion -> {
            OpinionDetailVO opinionDetailVO = new OpinionDetailVO();
            opinionDetailVO.setTitle(peopleOpinion.getTitle());
            opinionDetailVO.setConditions_content(peopleOpinion.getConditions_content());
            opinionDetailVO.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(peopleOpinion.getTime()));
            resultList.add(opinionDetailVO);
        });
        return resultList;
    }


    public List<MonthCountVO> newPeopleCount(String streetCode,String villageCode) {
        List<MonthCountVO> resultList = new ArrayList<>();
        //当前时间
        LocalDateTime now = LocalDateTime.now();
        //当年的第一天
        LocalDateTime januaryTime = now.with(TemporalAdjusters.firstDayOfYear());
        for (int i = 0; i < 12; i++) {
            LocalDateTime time = januaryTime.plusMonths(i);
            //查询条件
            Query query = new Query().
                    addCriteria(
                            Criteria.where("addtime").gte(DateSwitcher.getMinTime(time)).
                                    andOperator(Criteria.where("addtime").lte(DateSwitcher.getMaxTime(time.with(TemporalAdjusters.lastDayOfMonth())))).
                                    and("is_del").is(0).
                                    and("indicate_result").is(1)
                    );
            if (streetCode != null && streetCode.length() > 0 && !streetCode.equals("")) {
                query.addCriteria(Criteria.where("streetCode").is(streetCode));
            }
            if (villageCode != null && villageCode.length() > 0 && !villageCode.equals("")) {
                query.addCriteria(Criteria.where("villageCode").is(villageCode));
            }
            //查询每月活动数量
            long count = newPersonRepository.count(query);
            MonthCountVO monthCountVO = new MonthCountVO();
            monthCountVO.setMonth(time.getMonthValue());
            monthCountVO.setCount(count);
            resultList.add(monthCountVO);
        }
        return resultList;
    }

    public List<NewPeopleDetailVO> newPeopleDetail(String streetCode,String villageCode) {
        ArrayList<NewPeopleDetailVO> resultList = new ArrayList<>();
        //获取今年的开始时间和结束时间
        LocalDateTime now = LocalDateTime.now();
        int year = now.getYear();
        LocalDateTime startTime = LocalDateTime.of(year, 1, 1, 00, 00, 00);
        LocalDateTime endTime = LocalDateTime.of(year, 12, 31, 23, 59, 59);
        //查询今年的发展成员
        //设置查询条件
        Query query = new Query().addCriteria(
                Criteria.where("addtime").gte(startTime).
                        andOperator(Criteria.where("addtime").lte(endTime)).
                        and("is_del").is(0)).
                with(Sort.by(Sort.Direction.ASC, "addtime"));
        if (streetCode != null && streetCode.length() > 0 && !streetCode.equals("")) {
            query.addCriteria(Criteria.where("streetCode").is(streetCode));
        }
        if (villageCode != null && villageCode.length() > 0 && !villageCode.equals("")) {
            query.addCriteria(Criteria.where("villageCode").is(villageCode));
        }
        //查询今年所有的社情名义
        List<NewPerson> peopleOpinionList = newPersonRepository.findAll(query);
        peopleOpinionList.forEach(newPerson -> {
            NewPeopleDetailVO newPeopleDetailVO = new NewPeopleDetailVO();
            newPeopleDetailVO.setName(newPerson.getName());
            newPeopleDetailVO.setPartyTypeName(sysDictService.getValueByType(SysDictTypeKeyConst.PUBLIC_PARTY_TYPE, newPerson.getPartyTypeId()));
            newPeopleDetailVO.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(newPerson.getAddtime()));
            newPeopleDetailVO.setIndicate_result(newPerson.getIndicate_result().toString());
            resultList.add(newPeopleDetailVO);
        });
        return resultList;
    }

    public List getPartyType() {
        SysDict sysDict = sysDictRepository.findOne(new Query().addCriteria(Criteria.where("isDelete").is(0).and("code").is("public_party_type")));
        String id = sysDict.getId();
        List<SysDict> resultList = sysDictRepository.findAll(new Query().addCriteria(Criteria.where("isDelete").is(0).and("pid").is(id)));
        return resultList;
    }

    public List getPartyinfoList(Integer type) {
        List<PartyMapListVO> resultList = new ArrayList();
        List<Partyinfo> partyinfoList = partyinfoRepository.findAll(new Query().addCriteria(Criteria.where("is_del").is(0).and("type").is(type)));
        partyinfoList.forEach(partyinfo -> {
            PartyMapListVO partyMapListVO = new PartyMapListVO();
            partyMapListVO.setName(partyinfo.getName());
            partyMapListVO.setInfo(partyinfo.getIntroduction());
            partyMapListVO.setLatitude(partyinfo.getLatitude());
            partyMapListVO.setLongtitue(partyinfo.getLongitude());
            partyMapListVO.setUrl(partyinfo.getPicture());
            partyMapListVO.setTime(new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(partyinfo.getTime()));
            resultList.add(partyMapListVO);
        });
        return resultList;
    }

    public Integer addPartyinfo(String name, String address, String introduction, String picture, String latitude, String longitude, Integer type) {
        Partyinfo partyinfo = new Partyinfo();
        SnowFlakeUtils snowFlake = new SnowFlakeUtils(2, 2);
        String s = String.valueOf(snowFlake.nextId());
        partyinfo.set_id(s);
        partyinfo.setId(s);
        partyinfo.setIs_del(0);
        partyinfo.setTime(new Date());
        partyinfo.setName(name);
        partyinfo.setAddress(address);
        partyinfo.setIntroduction(introduction);
        partyinfo.setPicture(picture);
        partyinfo.setLatitude(latitude);
        partyinfo.setLongitude(longitude);
        partyinfo.setType(type);
        Partyinfo save = partyinfoRepository.save(partyinfo);
        if (save != null) {
            return 1;
        }
        return 0;
    }
}
